﻿namespace Resharper.Abstractions.CodeDom.Types.Classes
{
  using System;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Psi.CSharp.Tree;
  using Resharper.Abstractions.Actions;
  using Resharper.Abstractions.CodeDom.Expressions.LiteralExpressions;

  /// <summary>Defines the <see cref="LiteralExpressionExtensions"/> class.</summary>
  public static class ClassExtensions
  {
    #region Public Methods

    /// <summary>Gets the name of the class by.</summary>
    /// <param name="context">The context.</param>
    /// <param name="className">Name of the class.</param>
    /// <returns>Returns the class by name.</returns>
    public static Class GetClassByName([NotNull] this ExecuteContext context, [NotNull] string className)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      if (className == null)
      {
        throw new ArgumentNullException("className");
      }

      var @class = context.GetTypeByName(className) as IClassDeclaration;

      return @class != null ? new Class(@class) : null;
    }

    /// <summary>Gets the selected literal expression.</summary>
    /// <param name="context">The context.</param>
    /// <returns>Returns the selected literal expression.</returns>
    [CanBeNull]
    public static Class GetContainingClass([NotNull] this ProviderContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      var classDeclaration = context.InternalGetSelectedElement<IClassDeclaration>();

      return classDeclaration != null ? new Class(classDeclaration) : null;
    }

    #endregion
  }
}